//==============================================================================
#include "TextBox.h"

string const ui::TextBox::TEXTBOX_TYPE = "TEXT_BOX";
//==============================================================================
ui::TextBox::TextBox(int posX, int posY, int width, int height) : Component(posX, posY, width, height) {
	SDL_EnableUNICODE( SDL_ENABLE );
	this->type = TEXTBOX;
	this->active = false;
	this->cursor = false;
	this->box.x = posX;
	this->box.y = posY;
	this->box.w = width;
	this->box.h = height;
	this->font = TTF_OpenFont("Arial.ttf", TAM_FONT);
	if(!this->font)
    {
        std::string mensaje("TextBox - Error en TTF_OpenFont: ");
        mensaje += TTF_GetError();
        throw(SDL_Exception( mensaje));
    }
	SDL_Color textColor = { 0, 0, 0};
	this->textColor = textColor;
	this->selected = false;
	this->showText = "";
}
//------------------------------------------------------------------------------
ui::TextBox::TextBox( int posX, int posY, int width, int height, std::string text) : Component(posX, posY, width, height){
	SDL_EnableUNICODE( SDL_ENABLE );
	this->type = TEXTBOX;
	this->active = false;
	this->selected = false;
	this->box.x = posX;
	this->box.y = posY;
	this->box.w = width;
	this->box.h = height;
	this->font = TTF_OpenFont("Arial.ttf", TAM_FONT);
    if(!this->font)
    {
        std::string errorMessage("TEXTBOX - TTF_OpenFont - ERROR");
        throw(SDL_Exception(errorMessage));
    }
	SDL_Color textColor = { 0, 0, 0};
	this->textColor = textColor;
	this->showText = text;
}
//------------------------------------------------------------------------------
void ui::TextBox::setText(std::string text){
	this->showText=text;
}
//------------------------------------------------------------------------------
std::string ui::TextBox::getText(void){
	return this->showText;
}
//------------------------------------------------------------------------------
string ui::TextBox::getType(void){
	return TEXTBOX_TYPE;
}
//------------------------------------------------------------------------------
void ui::TextBox::setSelected(bool select){
	this->selected = select;
}
//------------------------------------------------------------------------------
void ui::TextBox::clearBox(){
	this->showText.clear();
}
//------------------------------------------------------------------------------
void ui::TextBox::drawBox(SDL_Surface* s) {
	if (this->selected)
	{
		if(SDL_FillRect( s, &this->box, ORANGE))
            throw SDL_Exception( std::string("TEXTBOX - SDL_FillRect - ERROR"));
	}
	else
	{
		if(SDL_FillRect( s, &this->box, WHITE))
            throw SDL_Exception( std::string("TEXTBOX - SDL_FillRect - ERROR"));
	}
}
//------------------------------------------------------------------------------
void ui::TextBox::draw(SDL_Surface* s) {
    try
    {
        this->drawBox(s);
        this->drawText(s);
    }
    catch(SDL_Exception &ex)
    {
        throw ex;
    }
}
//------------------------------------------------------------------------------
void ui::TextBox::drawText(SDL_Surface* s){

	this->drawBox(s);
    std::string display = "";
    display = this->showText;
    SDL_Surface* message = NULL;
    message = TTF_RenderText_Solid(this->font, display.c_str(), this->textColor);
	if (message != NULL)
		this->textWidth=message->w;

    SDL_Rect tmp = this->box;
	SDL_Rect tmp2;
    tmp.x +=BEGIN_SPACE;
	//Check width of the renderized text to show compared with box width.
	if (message != NULL){
		tmp2 = message->clip_rect;
		int dif = tmp2.w - this->box.w;
		if (dif >= -END_SPACE){
			tmp2.x+=dif+END_SPACE;
			tmp2.w =tmp.w;}
		if(SDL_BlitSurface(message, &tmp2, s, &tmp))
		{
            std::string errorMessage("TEXTBOX - BLIT SURFACE MESSAGE - ERROR");
            throw SDL_Exception (errorMessage);
		}
	}
	else
		SDL_BlitSurface(message, NULL, s, &tmp);

	if(this->active){
		if (message !=NULL){
			this->drawCursor(tmp.x + tmp.w, s);
		} else
			this->drawCursor(tmp.x, s);
	}
	SDL_FreeSurface(message);
}
//------------------------------------------------------------------------------
void ui::TextBox::drawCursor(int pos, SDL_Surface* s){
		Draw_Line(s,pos, this->box.y+1,pos, this->box.y+this->box.h-3, BLACK);
}

//------------------------------------------------------------------------------
bool ui::TextBox::isClicked(SDL_Event* e){
	if(e->type == SDL_MOUSEBUTTONDOWN && e->button.button == SDL_BUTTON_LEFT) {
        int x = e->button.x;
        int y = e->button.y;
        if(x < this->box.w+this->box.x && x > this->box.x && y < this->box.h + this->box.y && y > this->box.y) {
            return  true;
		} else {
            return  false;
        }
	} else
		return false;
}
//------------------------------------------------------------------------------
COMPONENT_TYPE ui::TextBox::handleEvent(SDL_Event* ev) {
	//If left click the textbox gets active for input.
    if(ev->type == SDL_MOUSEBUTTONDOWN && ev->button.button == SDL_BUTTON_LEFT) {
        int x = ev->button.x;
        int y = ev->button.y;
        if(x < this->box.w+this->box.x && x > this->box.x && y < this->box.h + this->box.y && y > this->box.y) {
            this->active = true;
			this->clearBox();
        } else {
            this->active = false;
        }
    } else
		if(ev->type == SDL_KEYDOWN && this->active) {
		//If the key is a space
		if( ev->key.keysym.unicode == (Uint16)' ' ) {
			//Append the character
			this->showText += (char)ev->key.keysym.unicode;
		} else
			//If the key is a number or a symbol.
			if( ( ev->key.keysym.unicode >= (Uint16)'!' ) && ( ev->key.keysym.unicode <= (Uint16)'_' ) ) {
				this->showText += (char)ev->key.keysym.unicode;
			}
			//If the key is a uppercase letter
			else
				if( ( ev->key.keysym.unicode >= (Uint16)'A' ) && ( ev->key.keysym.unicode < (Uint16)'Z' ) ) {
					this->showText += (char)ev->key.keysym.unicode;
				}
			//If the key is a lowercase letter
				else
					if( ( ev->key.keysym.unicode >= (Uint16)'a' ) && ( ev->key.keysym.unicode <= (Uint16)'z' ) ) {
						this->showText += (char)ev->key.keysym.unicode;
					}
		if( ( ev->key.keysym.sym == SDLK_BACKSPACE ) && ( this->showText.length() != 0 ) ) {
			//Remove a character from the end
			this->showText.erase( this->showText.length() - 1 );
		}
	}
	return this->type;
}
//------------------------------------------------------------------------------
ui::TextBox::~TextBox(void){
	SDL_EnableUNICODE( SDL_DISABLE );
	TTF_CloseFont( this->font );
}
//==============================================================================